import { CreateModalList } from '@/biz-components/hoc-form/with-list';
import { xhrPostJson } from '@/services';
import { getUserInfo } from '@/utils/userInfo';
import { getValidateMessage } from '@/utils/validate-rules/message';
import { onFieldValueChange } from '@formily/core';
import { message, notification } from 'antd';
import { useRef, useState } from 'react';
import { history } from "umi";
import ProcessModal from './process-m';
import api from './services';
import { useVisible } from '@/biz-components/modals/hooks';

const WithListComp = CreateModalList({});

const ArchivesManageList = (props: any) => {
  const formRef = useRef();
  const { match } = props;
  const [refresh, setRefresh] = useState(false);
  const [visible, showModal, hiddenModal] = useVisible(false);
  const [prcsInstId, setPrcsInstId] = useState('');
  const { userCode } = getUserInfo();

  return (
    <>
      <WithListComp
        match={match}
        history={history}
        reloadFlag={refresh}
        pageCode={'ArcvBrw_My_L'}
        pageApi={api.page}
        formatSearchData={(queryData: any) => {
          queryData.brwUser = userCode;

          return queryData;
        }}
        noModal
        noCache
        formConfigs={{
          effects: (form: any) => {
            formRef.current = form;
          },
        }}
        validator={{
          checkColBtns: (key, row) => {
            if (key === 'bianji') {
              return row.status === 'DRAFT';
            }
            if (key === 'liucheng') {
              return row.status !== 'DRAFT';
            }

            return true;
          }
        }}
        events={{
          buttonsEvents: {
            handleCreate: () => {
              history.push('/invoice/arcvBrw/create');
            }
          },
          tableButtonsEvents: {
            handleView: (key: any, row: any) => {
              history.push({
                pathname: '/invoice/arcvBrw/detail',
                query: {
                  code: row.code
                }
              });
            },
            handleEdit: (key: any, row: any) => {
              history.push(`/invoice/arcvBrw/edit?code=${row.code}`)
            },
            handleProcess: (key: any, row: any) => {
              showModal();
              if (!row.prcsInstId) {
                notification.error({
                  message: getValidateMessage('common.systemTip', true),
                  description: getValidateMessage('common.error.prcsInstId', true),
                });
                return;
              }
              setPrcsInstId(row.prcsInstId);
            }
          },
        }}
      />
      { visible && prcsInstId ? <ProcessModal cancel={hiddenModal} prcsInstId={prcsInstId} /> : null }
    </>
  );
};

export default ArchivesManageList;
